package cc.rengu.igas.bsps.core.service.trans;


import cc.rengu.igas.bsps.common.dao.TradeDetailInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.TradeDetailInfoMapperImpl;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.WithdrawalDetailInfoBean;
import cc.rengu.igas.bsps.facade.bean.WithdrawalSumInfoBean;
import cc.rengu.igas.bsps.facade.request.CashWithdrawalDetailQueryRequest;
import cc.rengu.igas.bsps.facade.response.CashWithdrawalDetailQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 交易提现明细列表查询
 * Created by yuhongwen
 */
public class CashWithdrawalDetailQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        CashWithdrawalDetailQueryRequest cashWithdrawalDetailQueryRequest = new CashWithdrawalDetailQueryRequest();
        ConvertUtil.convertOutput(cashWithdrawalDetailQueryRequest);
        return cashWithdrawalDetailQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        CashWithdrawalDetailQueryRequest cashWithdrawalDetailQueryRequest = (CashWithdrawalDetailQueryRequest) request;
        CashWithdrawalDetailQueryResponse cashWithdrawalDetailQueryResponse = new CashWithdrawalDetailQueryResponse();
        List<WithdrawalSumInfoBean> totalList = new ArrayList<>();
        String instId = cashWithdrawalDetailQueryRequest.getHeader().getInstId();
        String mchntNo = cashWithdrawalDetailQueryRequest.getMchntNo();
        String startDate = cashWithdrawalDetailQueryRequest.getStartDate();
        String endDate = cashWithdrawalDetailQueryRequest.getEndDate();
        String userId = cashWithdrawalDetailQueryRequest.getUserId();
        cashWithdrawalDetailQueryResponse.setPageIndex(cashWithdrawalDetailQueryRequest.getPageIndex());
        cashWithdrawalDetailQueryResponse.setPageSize(cashWithdrawalDetailQueryRequest.getPageSize());

        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(userId)
                || StringUtil.isEmptyOrNull(startDate) || StringUtil.isEmptyOrNull(endDate)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        //查询 虚拟账户余额提现
        TradeDetailInfoMapper tradeDetailInfoMapper = new TradeDetailInfoMapperImpl();
        List<MchntOrderInfo> mchntOrderInfolist = tradeDetailInfoMapper.selectCashWithdrawalMchntOrderGroupByDate(instId, mchntNo, startDate, endDate, null, null, null, "P01BS001");
        if (mchntOrderInfolist != null) {

            List<WithdrawalSumInfoBean> withdrawalSumInfoBeanList = new ArrayList<>();
            for (int i = 0; i < mchntOrderInfolist.size(); i++) {
                WithdrawalSumInfoBean withdrawalSumInfoBean = new WithdrawalSumInfoBean();
                MchntOrderInfo mchntOrderInfo = mchntOrderInfolist.get(i);
                withdrawalSumInfoBean.setTransAmt(mchntOrderInfo.getRemark1());
                withdrawalSumInfoBean.setTransNum(mchntOrderInfo.getRemark2());
                withdrawalSumInfoBean.setTxnDate(mchntOrderInfo.getTxnDate());
                withdrawalSumInfoBeanList.add(withdrawalSumInfoBean);
            }
            cashWithdrawalDetailQueryResponse.setWithdrawalSumInfoList(withdrawalSumInfoBeanList);
            /*查询商户订单（分页）*/
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageSize(cashWithdrawalDetailQueryRequest.getPageSize());
            pageInfo.setPageIndex(cashWithdrawalDetailQueryRequest.getPageIndex());
            PageResult pageResult = tradeDetailInfoMapper.selectMchntOrderPage(instId, mchntNo, startDate, endDate, null, pageInfo, "P01BS001");

            if (pageResult == null || pageResult.getResult().isEmpty()) {

                rglog.info("根据查询条件，数据为0条!");
                cashWithdrawalDetailQueryResponse.setTotalPage(0);
                cashWithdrawalDetailQueryResponse.setTotalRows(0);

            } else {

                List<WithdrawalDetailInfoBean> transDetailInfoBeanList = new ArrayList<>();
                for (Object object : pageResult.getResult()) {
                    MchntOrderInfo mchntOrderInfo = (MchntOrderInfo) object;
                    WithdrawalDetailInfoBean withdrawalDetailInfoBean = new WithdrawalDetailInfoBean();
                    withdrawalDetailInfoBean.setTxnDate(mchntOrderInfo.getTxnDate());
                    withdrawalDetailInfoBean.setTxnTime(mchntOrderInfo.getTxnTime());
                    withdrawalDetailInfoBean.setSysSeqNum(mchntOrderInfo.getSysSeqNum());
                    withdrawalDetailInfoBean.setTxnNum(mchntOrderInfo.getTxnNum());
                    withdrawalDetailInfoBean.setTxnName("虚拟账户余额提现");
                    if ("0".equals(mchntOrderInfo.getTransStatus()) || "1".equals(mchntOrderInfo.getTransStatus())) {
                        withdrawalDetailInfoBean.setWithdrawalStatus("1");
                    } else {
                        withdrawalDetailInfoBean.setWithdrawalStatus("2");
                    }
                    withdrawalDetailInfoBean.setWithdrawalAmt(mchntOrderInfo.getOrderAmt());
                    withdrawalDetailInfoBean.setAcctBalance(mchntOrderInfo.getVirtualAcctBalance());
                    withdrawalDetailInfoBean.setWithdrawalFee(mchntOrderInfo.getRealMchntFee());
                    withdrawalDetailInfoBean.setTransDesc(mchntOrderInfo.getOrderDesc());
                    transDetailInfoBeanList.add(withdrawalDetailInfoBean);
                }
                cashWithdrawalDetailQueryResponse.setWithdrawalDetailInfoList(transDetailInfoBeanList);
                cashWithdrawalDetailQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                cashWithdrawalDetailQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
            }

        } else {
            cashWithdrawalDetailQueryResponse.setTotalPage(0);
            cashWithdrawalDetailQueryResponse.setTotalRows(0);
        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(cashWithdrawalDetailQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        CashWithdrawalDetailQueryResponse cashWithdrawalDetailQueryResponse = (CashWithdrawalDetailQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(cashWithdrawalDetailQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     */
    private void convGroupMchntInfo(WithdrawalSumInfoBean withdrawalSumInfoBean, List<WithdrawalSumInfoBean> withdrawalSumInfoBeans) {
        WithdrawalSumInfoBean withdrawalSumInfoBean1 = new WithdrawalSumInfoBean();
        BeanUtil.beanCopy(withdrawalSumInfoBean, withdrawalSumInfoBean1);
        withdrawalSumInfoBeans.add(withdrawalSumInfoBean1);
    }


    /**
     * 根据日期生成查询日期list
     *
     * @param stime 开始日期
     * @param etime 截止日期
     */
    private List<String> findDates(String stime, String etime) {
        int offDay = DateUtil.compareDate(etime, stime);
        if (offDay < 0) {
            rglog.error("结束日期应大于开始日期!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        List<String> queryDays = new ArrayList<>();
        for (int m = 0; m <= offDay; m++) {
            queryDays.add(DateUtil.getBaseDateOffsetDay(stime, m));
        }
        return queryDays;
    }
}

